<?php
/**
 * 滚动图片 //1品牌宣传 2品牌活动 3商场宣传 4商场活动
 * @author Ice <iceinto@mallog.com.cn>
 */
class SlideModel extends BaseModel
{
    private $_count = 4;

    /**
     * 获取图
     * @param int $mid
     * @param int $bid
     * @param string $limit
     * @param string $order
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getList($map, $limit = '0,4', $order = 'rand()', $field = 'DISTINCT bid')
    {
        $ads = $this->field($field)->where($map)->limit($limit)->order($order)->select();
        $adss = array();
        if ($ads) {
            foreach ($ads as $key => $v) {
                $map['bid'] = $v['bid'];
                $adss[] = $this->field('bid, id, mid, fid, title, image')->where($map)->order($order)->find();
            }
        }
        if ($adss) {
            $this->setImage(&$adss);
        }
        return $adss;
    }

    /**
     * 设置图片信息
     * @param array $ads 列表数组
     * @parem blean $isM 是否是维护图片
     * @author Ice <iceinto@mallog.com.cn>
     */
    private function setImage($ads, $isM = false)
    {
        if ($ads) {
            foreach ($ads as $key => $ad) {
                if ($isM) {
                    $ads[$key] = $this->getBrandMaterialSlide($ad);
                } else {
                    $ads[$key]['image_url'] = $this->getManagedUrl($ad['image'], 'slide');
                }
                $ads[$key]['ad_url'] = $this->getAdUrl($ad['mid'], $ad['fid'], $ad['bid']);
            }
        }
    }

    /**
     * 获取首页滚动图
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getIndexList()
    {
        $map = array();
        $slide1 = array();
        $slide2 = array();
        $time = $this->getThisDayTime();
        $map['type'] = 2;
        $map['start_time'] = array('elt', $time);
        $map['end_time'] = array('egt', $time);
        //获取品牌宣传
        $slide1 = $this->getList($map);
        if (count($slide1) < $this->_count) {
            //随机获取品牌
            $slide2 = M('Brand')->field('DISTINCT id as bid')
                ->where($map)
                ->limit('0,' . ($this->_count - count($slide1)))
                ->order('rand()')
                ->select();

            if (!empty($slide2)) {
                $this->setImage(&$slide2, true); //获取地址
            }
        }
        return $this->getSlide($slide1, $slide2);
    }

    /**
     * 获取商场滚动图
     * @param int $id
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getMallList($id)
    {
        if ($id) {
            $map = array();
            $slide1 = array();
            $slide2 = array();
            $time = $this->getThisDayTime();
            $map['mid'] = $id;
            $map['type'] = 4;
            $map['start_time'] = array('elt', $time);
            $map['end_time'] = array('egt', $time);
            //获取顺序商场活动/品牌活动/商场宣传/品牌宣传
            //获取品牌活动
            $slide1 = $this->getList($map);
            if (count($slide1) < $this->_count) {
                unset($map['start_time']);
                unset($map['end_time']);
                $map['type'] = 3;
                $slide2 = $this->getList($map);
            } else {
                return $slide1;
            }
            $slide = $this->getSlide($slide1, $slide2);
            if (count($slide) >= $this->_count) {
                return $this->getSlide($slide, $this->getIndexList($id));
            } else {
                unset($map);
                $map['mid'] = $id;
                $slide2 = M('FloorBrand')->field('DISTINCT bid,mid,fid')
                    ->where($map)
                    ->limit('0,' . ($this->_count - count($slide)))
                    ->order('rand()')
                    ->select();

                if (!empty($slide2)) {
                    $this->setImage(&$slide2, true); //获取地址
                    $slide = $this->getSlide($slide, $slide2);
                }
            }
            return $slide;
        }
        return false;
    }

    /**
     * 根据品牌获取一张品牌展示图
     * @param $bid
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getBrandOne($bid, $mid = 0, $fid = 0)
    {
        $map = array();
        $time = $this->getThisDayTime();
        $map = array();
        if (!empty($mid)) {
            $map['mid'] = $mid;
        }
        if (!empty($fid)) {
            $map['fid'] = $fid;
        }
        $map['bid'] = $bid;
        $map['type'] = 2;
        $map['start_time'] = array('elt', $time);
        $map['end_time'] = array('egt', $time);
        $sl = $this->getList($map, '0,1');
        if (count($sl) < 1) {
            unset($map);
            $ad = array();
            $ad['bid'] = $bid;
            $sl = $this->getBrandMaterialSlide($ad);
        } else {
            $sl = $sl[0];
            $sl['image_url'] = $this->getManagedUrl($sl['image'], 'slide');
        }
        if ($sl) {
            $sl['ad_url'] = $this->getAdUrl($sl['mid'], $sl['fid'], $sl['bid']);
            return $sl;
        } else {
            return false;
        }

    }

    /**
     * 合并广告结果
     * @param array $slide1
     * @param array $slide2
     * @author Ice <iceinto@mallog.com.cn>
     */
    private function getSlide($slide1, $slide2)
    {
        $slide = array();
        if ($slide1 && $slide2) {
            $slide = array_merge($slide1, $slide2);
        } else if ($slide1 && !$slide2) {
            $slide = $slide1;
        } else if (!$slide1 && $slide2) {
            $slide = $slide2;
        }
        $slide = array_slice($slide, 0, 4);
        return $slide;
    }

    //获取素材库里面的图片
    /**
     * 获取制定品牌一张展示图
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getBrandMaterialSlide($ad)
    {
        $imagUrl = '';
        if (empty($ad)) {
            return false;
        }
        $data = $this->getAdMFB(null, null, $ad['bid']);
        /**
         *  获取形式为:
         *   img.mallog.cn/b/s/品牌英文名
         *   获取对应目录下显得品牌展示图是否存在
         *   3～5月为春季
         *   6～8月为夏季
         *   9～11月为秋季
         *   12～2月为冬季
         *  如果四季都没有图片就显示默认图片一张
         */
        if (empty($data)) {
            return false;
        } else {
            $ad['image_url'] = strtolower(SITE_URL . '/b/s/' . $data['brand']['ename']);
            $ad['title'] = $data['brand']['ename'];
        }
        return $ad;
    }
}

?>